package myPackage;

import java.util.Hashtable;
import java.util.Map;

import net.jxta.document.Advertisement;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.platform.NetworkConfigurator;
import net.jxta.platform.NetworkManager;
import nox.net.group.GroupConnectionHandler;
import nox.net.peer.PeerConnectionHandler;
import nox.ui.common.NoxJListItem;

/**
 */
public class myToolkit {
	/**
	 */
//	public class HuntingEventHandler extends DiscoveryEventHandler{
//		AdvTable table;
//		public HuntingEventHandler(AdvTable tab){
//			table = tab;
//		}
//		public void setAdvTable(AdvTable tab){
//			table = tab;
//		}
//		public void eventOccured(AdvTable searchResultTable, Advertisement adv, Object src, long delay) {
//			try{
//				searchResultTable.addRow(adv, src, delay);
//			}catch(Exception ex){
//				ex.printStackTrace();
//			}
//		}
//		@Override
//		public void eventOccured(Advertisement adv, Object src, long delay) {
//			eventOccured(table, adv, src, delay);
//		}
//	}
//	/**
//	 */
//	public class CheckStatusEventHandler extends DiscoveryEventHandler{
//		NoxJListItem listItem;
//		
//		public CheckStatusEventHandler(NoxJListItem item){
//			listItem = item;
//		}
//		public void setItem(NoxJListItem it){
//			listItem = it;
//		}
//
//		@Override
//		public void eventOccured(Advertisement adv, Object src, long delay) {
//			eventOccured(listItem, adv, src, delay);
//		}
//
//		public void eventOccured(NoxJListItem item, Advertisement adv,Object src, long delay) {
//			/*try{
//				item.setStatus(adv);
//			}catch(Exception ex){
//				ex.printStackTrace();
//			}*/
//		}
//	}
//	
	private static JXTANetwork network;
	private static NetworkManager manager;
	private static NetworkConfigurator configer;
	//private static AdvHunter advhunter;
	private static HuntingEventHandler hehandler;
	private static CheckStatusEventHandler cshandler;

	
	private static transient Map<PeerID, PeerConnectionHandler> pconnHdlerCache = new Hashtable<PeerID, PeerConnectionHandler>();
	private static transient Map<PeerGroupID, GroupConnectionHandler> gconnHdlerCache = new Hashtable<PeerGroupID, GroupConnectionHandler>();
	
	private static float Opacity = 100;
	
	public Toolkit(){ }
	
	public Toolkit(JXTANetwork nw, NetworkManager mng, NetworkConfigurator conf, /*AdvHunter ah,*/ HuntingEventHandler heh, CheckStatusEventHandler csh){
		network = nw;
		manager = mng;
		configer = conf;
		hehandler = heh;
		cshandler = csh;
	}
	
	public static JXTANetwork getNetwork(){
		return network;
	}
	public static NetworkManager getNetworkManager(){
		return manager;
	}
	public static NetworkConfigurator getNetworkConfigurator(){
		return configer;
	}
	/*public AdvHunter getAdvHunter(){
		return advhunter;
	}*/
	public static HuntingEventHandler getHuntingEventHandler(){
		return hehandler;
	}
	public static CheckStatusEventHandler getCheckStatusEventHandler(){
		return cshandler;
	}
	

	public static float getOpacity(){
		return Opacity;
	}
	public static void setOpacity(float opa){
		Opacity = opa;
	}


	public static PeerConnectionHandler getPeerConnectionHandler(PeerID pid){
		if(pconnHdlerCache.containsKey(pid)){
			return pconnHdlerCache.get(pid);
		}
		return null;
	}
	/**
	 */
	public static boolean registerPeerConnectionHandler(PeerID pid, PeerConnectionHandler handler){
		if(pid != null && handler != null
				&& !pconnHdlerCache.containsKey(pid)){
			pconnHdlerCache.put(pid, handler);
			return true;
		}
		return false;
	}
	/**
	 */
	public static boolean forceRegisterPeerConnectionHandler(PeerID pid, PeerConnectionHandler handler){
		if(pid != null && handler != null){
			pconnHdlerCache.put(pid, handler);
			return true;
		}
		return false;
	}

	public static GroupConnectionHandler getGroupConnectionHandler(PeerGroupID gid){
		if(gconnHdlerCache.containsKey(gid)){
			return gconnHdlerCache.get(gid);
		}
		return null;
	}
	/**
	 */
	public static boolean registerGroupConnectionHandler(PeerGroupID gid, GroupConnectionHandler handler){
		if(gid != null && handler != null
				&& !gconnHdlerCache.containsKey(gid)){
			gconnHdlerCache.put(gid, handler);
			return true;
		}
		return false;
	}
	/***/
	public static boolean forceRegisterGroupConnectionHandler(PeerGroupID gid, GroupConnectionHandler handler){
		if(gid != null && handler != null){
			gconnHdlerCache.put(gid, handler);
			return true;
		}
		return false;
	}
}